package mongo;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import org.apache.commons.lang.time.DateUtils;
import org.bson.Document;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * USER: lintc 【lintiancong@zhuojianchina.com】
 * DATE: 2017-04-07 14:57
 */
public class MongoWash {

    public static void main(String[] args) {
        Date time = DateUtil.getyyyy_MM_dd("2017-03-23");
        String times;
        for (int i = 0; i < 12; i++) {
            time = DateUtils.addDays(time, 1);
            times = DateUtil.dateToString(time, "yyyy-MM-dd");
            washApiLog(times);
        }
//        washApiLog("2017-03-23");
    }

    /**
     * 转移apilog表数据
     * @param time
     */
    public static void washApiLog(String time) {
        // from
        MongoCollection<Document> collection = MongoTest.getCollection("api_log");
        // end
        MongoCollection<Document> test = MongoTest.getCollection("api_log_" + time);
        String timeS = time;
        System.out.println(timeS);
        DBObject obj = new BasicDBObject();
        DBObject timeObj = new BasicDBObject("$regex", timeS); // 错误日期
        timeObj.put("$options", "i");
        obj.put("time", timeObj);
        FindIterable<Document> findIterable = collection.find(new Document("time", timeObj));
        MongoCursor<Document> mongoCursor = findIterable.iterator();
        int i = 0;
        List<Document> list = new ArrayList<>();
        Document d;
        long s = System.currentTimeMillis();
        while (mongoCursor.hasNext()) {
            d = mongoCursor.next();
            d.remove("_id");
            list.add(d);
            if (list.size() == 20) {
                ++i;
                test.insertMany(list);
                list = new ArrayList<>();
                System.out.println((System.currentTimeMillis() - s) + " : " + i);
                s = System.currentTimeMillis();
            }
            //            System.out.println((++i) + " : " + mongoCursor.next().toJson());
        }
    }

    public static void wash() {
        MongoCollection<Document> collection = MongoTest.getCollection("TEST_2017-04-05");
        MongoCollection<Document> test = MongoTest.getCollection("api_log_2017-04-05");
        //        List<JSONObject> list = getCatchList("app_shdsyy", "app-101-shdsyy");
        //        insertList(collection, list);
        String timeS = DateUtil.dateToString(DateUtils.addDays(new Date(), -1), "yyyy-MM-dd");
        System.out.println(timeS);
        DBObject obj = new BasicDBObject();
        DBObject timeObj = new BasicDBObject("$regex", timeS); // 错误日期
        timeObj.put("$options", "i");
        obj.put("time", timeObj);
        FindIterable<Document> findIterable = collection.find(new Document("time", timeObj));
        MongoCursor<Document> mongoCursor = findIterable.iterator();
        int i = 0;
        List<Document> list = new ArrayList<>();
        Document d;
        long s = System.currentTimeMillis();
        while (mongoCursor.hasNext()) {
            d = mongoCursor.next();
            d.remove("_id");
            list.add(d);
            if (list.size() == 20) {
                ++i;
                test.insertMany(list);
                list = new ArrayList<>();
                System.out.println((System.currentTimeMillis() - s) + " : " + i);
                s = System.currentTimeMillis();
            }
            //            System.out.println((++i) + " : " + mongoCursor.next().toJson());
        }
    }
}
